package com.example.pluto.common.handler;

import com.example.pluto.common.exceptions.ServiceException;
import com.example.pluto.common.models.Ret;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@Component
@RestControllerAdvice()
public class GlobalExceptionHandler {


    /**
     * 自定义返回状态
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = ServiceException.class)
    public Ret handleException(ServiceException e) {
        log.error("ServiceException 系统内部异常，异常信息", e);
        return Ret.error(e.getCode(), e.getMessage(), null);
    }

    @ExceptionHandler(value = RuntimeException.class)
    public Ret handleException(RuntimeException e) {
        log.error("RuntimeException 系统内部异常，异常信息", e);
        return Ret.error(500, e.getMessage(), null);
    }

    @ExceptionHandler(value = Exception.class)
    public Ret handleException(Exception e) {
        log.error("Exception 系统内部异常，异常信息", e);
        return Ret.error("系统内部异常，异常信息：" + e.getMessage());
    }
}
